package com.cz.run.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO()
 * @auther:CAIYJ
 * @Date:2018/5/11 15:16
 */
@Configuration
@EnableSwagger2
@Profile({"dev", "test"})
public class SwaggerConfig extends WebMvcConfigurationSupport {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cz.run.web"))
                .paths(PathSelectors.any()).build()
                .globalOperationParameters(parameter());
    }

    /**
     * 全局参数
     *
     * @return
     */
    private List<Parameter> parameter() {
        List<Parameter> params = new ArrayList<>();
        params.add(new ParameterBuilder().name("token").description("用户token")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build());
        return params;

    }

    @SuppressWarnings("deprecation")
    private ApiInfo apiInfo() {
        Contact contact = new Contact("区块链项目", "", "");
        return new ApiInfoBuilder().title("区块链项目")
                .description("区块链项目")
                .termsOfServiceUrl("http://www.baidu.com").contact(contact)
                .version("2.0").build();
    }
}
